﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ScoreMS.Views
{
    /// <summary>
    /// SyllabusAdd.xaml 的交互逻辑
    /// </summary>
    public partial class SyllabusAdd : Page
    {
        public SyllabusAdd()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(SyllabusAdd_Loaded);
        }

        void SyllabusAdd_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbxCourse.ItemsSource = BLL.CourseBS.GetCourseList();
        }

        /// <summary>
        /// 当选择课程改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxCourse_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            updateDataGrid();
        }

        private void updateDataGrid()
        {
            if (cbxCourse.SelectedIndex == -1) return;
            var model = cbxCourse.SelectedItem as Model.Course;
            this.dataGrid1.ItemsSource = BLL.Course_syllabusBS.GetCourse_syllabusList(model.course_id);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (cbxCourse.SelectedIndex == -1)
            {
                MessageBox.Show("请先选择课程！", "提示");
            }
            if (txtsyllabus_descr.Text.Trim() == "")
            {
                MessageBox.Show("请填写完整！", "提示");
                return;
            }
            if (BLL.Course_syllabusBS.AddCourse_syllabus(new Model.Course_syllabus(){
                course_id = (cbxCourse.SelectedItem as Model.Course).course_id,
                syllabus_descr = txtsyllabus_descr.Text.Trim(),
            }))
            {
                MessageBox.Show("恭喜！添加成功！", "提示");
                updateDataGrid();   //更新DataGrid
            }
            else
            {
                MessageBox.Show("添加失败！请确保输入数据正确！", "警告");
            }
        }

    }
}
